/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.StockDao) SALE.dao.local.StockDao = function(){
	var stockDoc;
}

SALE.dao.local.StockDao.prototype = {
		/**
		 * 
		 */
		initStockDoc : function() {
			if(!this.stockDoc) {
				this.stockDoc = LocalXMLFileUtil.getStockDoc();
			}
			
			return this.stockDoc;
		},
		
		saveStockDoc : function() {
			LocalXMLFileUtil.saveXML(this.stockDoc,LocalXMLFileUtil.getStockFile());
		},
		
		insertStockInfo : function(stockJson) {
			var root = this.stockDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			stockJson["id"] = id;
			root.setAttribute("maxid",id);
			var stockNode = this.stockDoc.createElement("stock");
			
			this.updateStock(stockJson, stockNode);			
			root.appendChild(stockNode);
			
			this.saveStockDoc();
		},
		
		modifyStockInfo : function(stockJson) {
			var stockNode = this.searchStockByID(stockJson.id);
			
			this.updateStock(stockJson, stockNode);
			
			this.saveStockDoc();
		},
		
		deleteStockInfo : function(id) {
			var stockNode = this.searchStockByID(id);
			var root = this.stockDoc.documentElement;
			root.removeChild(stockNode);
		},
	
		addStock : function(stockJson) {
			var stock = this.searchStockByCode(stockJson.sname, stockJson.pcode);
			if(stock) {
				var numberNode = Ext.query("/number/*", stock)[0];
				if(numberNode) {
					var snumber = numberNode.nodeValue;
					var number = parseFloat(snumber) + parseFloat(stockJson.number);
					
					//alert(snumber + ":" + number);
					
					this.updateStock({'number':number}, stock);
					this.saveStockDoc();
				}
					
			} else {
				this.insertStockInfo(stockJson);
			}
		},
		
		updateStock : function(stockJosn, stockNode) {
			
			for(var attri in stockJosn) {
				var attriNodes = Ext.DomQuery.select("/" + attri, stockNode);
				if(attriNodes != null && attriNodes.length > 0) {
					stockNode.removeChild(attriNodes[0]);
				}
				
				var attriNode = this.stockDoc.createElement(attri);
				var contextNode = this.stockDoc.createCDATASection(stockJosn[attri]);
				attriNode.appendChild(contextNode);
				stockNode.appendChild(attriNode);
			}
		},
		
		searchStockByID : function(id) {
			var root = this.stockDoc.documentElement;
			var objArray = Ext.query("stock:has(id:nodeValue(" + id+ "))", root);
			var result;
				
			if(objArray.length == 1) {
				result = objArray[0];
			}	
			
			return result;
		},
		
		searchStockByCode : function(sname, pcode) {
			var root = this.stockDoc.documentElement;
			var objArray = Ext.query("stock:has(pcode:nodeValue(" + pcode+ "))", root);
			var result;
				
			Ext.each(objArray, function(stock){
				var scodeArray = Ext.query("/sname:nodeValue(" + sname+ ")", stock);
				if(scodeArray.length == 1) {
					result = stock;
				}
			});			
			
			return result;
		},
		
		searchStockJsonByID : function(id) {
			
			var resultNode = this.searchStockByCode(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		searchStockByCondition : function(condition) {
			var root = this.initStockDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/stock", root);
				var stocksJson = {stocks: []};
				
				Ext.each(objArray, function(stock) {
					var filterFlag = false;
					
					if (condition) {
						for (var attri in condition) {
							var node = Ext.query("/" + attri + "/*", stock)[0];
							var nodeValue = !node ? "" : node.nodeValue;
							var condValue = condition[attri];
							
							if (attri == "number") {
								
								if (condValue == 0 && Ext.num(nodeValue, 0) > 0) {
									filterFlag = true;
									break;
								} else if (condValue == 1 && Ext.num(nodeValue, 0) <= 0) {
									filterFlag = true;
									break;
								}
							} else {
								if (nodeValue.toUpperCase().indexOf(condValue.toUpperCase()) < 0) {
									filterFlag = true;
									break;
								}
							}
						}
					}
					
					if (!filterFlag) {
						var children = stock.childNodes;
						var stockJson = {};
						for (var i = 0; i < children.length; i++) {
							var child = children[i];
							
							if (child.nodeType == 1) {
								if (!stockJson[child.nodeName] && child.childNodes[0] != null) {
									stockJson[child.nodeName] = child.childNodes[0].nodeValue;
								}
							}
						}
						stocksJson.stocks.push(stockJson);
					}
				});
				
				return stocksJson;
			}
		},
		
		buildStocksJson : function() {
			var root = this.initStockDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/stock", root);
				var stocksJson = {stocks: []};
				
				Ext.each(objArray, function(stock) {
					var children = stock.childNodes;
					var stockJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!stockJson[child.nodeName] && child.childNodes[0] != null) {
								stockJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					stocksJson.stocks.push(stockJson);
				});
				
				return stocksJson;
			}
		},
		
		searchWarnningStocksJson : function() {
			var root = this.initStockDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/stock", root);
				var stocksJson = [];
				
				Ext.each(objArray, function(stock) {
					
					var nNode = stock.getElementsByTagName("number");
					var unNode = stock.getElementsByTagName("upper_limit");
					var lnNode = stock.getElementsByTagName("lower_limit");
					
					var number = 0, uNumber= 2147483647, lNumber= 0;
					if(nNode != null && nNode.length > 0) {
						number = nNode[0].childNodes[0].nodeValue == "" ? 0 : parseFloat(nNode[0].childNodes[0].nodeValue);
					}
					
					if(unNode != null && unNode.length > 0) {
						uNumber = unNode[0].childNodes[0].nodeValue == "" ? 2147483647 : parseFloat(unNode[0].childNodes[0].nodeValue);
					}
					
					if(lnNode != null && lnNode.length > 0) {
						lNumber = lnNode[0].childNodes[0].nodeValue == "" ? 0 : parseFloat(lnNode[0].childNodes[0].nodeValue);
					}
					
					if (lNumber > number || number > uNumber) {
						var children = stock.childNodes;
						var stockJson = {status: lNumber > number ? 1 : 2};
						for (var i = 0; i < children.length; i++) {
							var child = children[i];
							
							if (child.nodeType == 1) {
								if (!stockJson[child.nodeName] && child.childNodes[0] != null) {
									stockJson[child.nodeName] = child.childNodes[0].nodeValue;
								}
							}
						}
						stocksJson.push(stockJson);
					}
				});
				
				return stocksJson;
			}
		},
}